package com.css.dzj.dzjap.modules.project.generecord.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.annotation.SysLog;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.project.generecord.entity.Generecord;
import com.css.dzj.dzjap.modules.project.generecord.service.GenerecordService;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 一般工程备案
 *
 * @author liyuan
 */
@RestController
@RequestMapping("/generecord")
public class GenerecordController {
    @Autowired
    private GenerecordService generecordService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
    /**
     * 多条件分页
     *
     * @param pageNo
     * @param pageSize
     * @param proName
     * @param unitName
     * @param checkStatus
     * @param reportStatus
     * @param reCreateDepartment
     * @param reCreateUser
     * @param proAddress         项目地址，开发写为非,必需性false,正式为true
     * @return
     */
    @GetMapping("/getPage")
    public R getPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                     @RequestParam(name = "proName", required = false) String proName,
                     @RequestParam(name = "unitName", required = false) String unitName,
                     @RequestParam(name = "checkStatus", required = false) String checkStatus,
                     @RequestParam(name = "reportStatus", required = false) String reportStatus,
                     @RequestParam(name = "reCreateDepartment", required = false) String reCreateDepartment,
                     @RequestParam(name = "reCreateUser", required = false) String reCreateUser,
                     @RequestParam(name = "proAddress", required = false) String proAddress,
                     @RequestParam(name = "orgLevel", required = false) String orgLevel) {
        String s = "";
        try{
        // 根据项目厂址判断权限，先注释掉
            if (StringUtils.isEmpty(proAddress))
                return R.error("您没有权限！");
            PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
            QueryResults<Generecord> page = generecordService.getPage(proName, unitName, reCreateUser, checkStatus,
                    reportStatus, reCreateDepartment, proAddress, orgLevel, pageRequest);
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("多条件查询备案信息成功", Constant.GENERECORD, Constant.PT, Constant.PAGE, s, request);
            return R.ok().put("data", page);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("多条件查询备案信息失败", Constant.GENERECORD, Constant.PT, Constant.PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据ID查看详情
     *
     * @param
     * @return
     */
    @GetMapping("/getInfo")
    public R getGenerecordById(@RequestParam("Id") String reId) {
        String  s = "";
        try {
            Generecord generecord = generecordService.queryById(reId);
            s = JSON.toJSONString(reId);
            sysAuditService.saveSuccessAudit("备案详情查询成功", Constant.GENERECORD, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("data", generecord);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("备案详情查询失败", Constant.GENERECORD, Constant.PT, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 提交(暂存并且提交的合体)
     *
     * @param generecord
     * @return
     */
    @SysLog("一般工程备案：提交(暂存并且提交的合体)")
    @PostMapping("/add")
    public R add(@RequestBody Generecord generecord) {
        String s = "";
        try {
            // 1已提交，待审核
            generecord.setCheckStatus("1");
            generecordService.save(generecord);
            s = JSON.toJSONString(generecord);
            sysAuditService.saveSuccessAudit("添加成功", Constant.GENERECORD, Constant.PT, Constant.SUBMITANDSAVE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.GENERECORD, Constant.PT, Constant.SUBMITANDSAVE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 暂存即保存
     *
     * @param generecord
     * @return
     */
    @SysLog("一般工程备案：暂存")
    @PostMapping("/zcadd")
    public R zcadd(@RequestBody Generecord generecord) {
        String s = "";
        try {
            // 设置初始状态，0：未提交，暂存; 未上传
            generecord.setCheckStatus("0");
            generecordService.save(generecord);
            s = JSON.toJSONString(generecord);
            sysAuditService.saveSuccessAudit("添加成功", Constant.GENERECORD, Constant.PT, Constant.ZCADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.GENERECORD, Constant.PT, Constant.ZCADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     *
     * @param generecord
     * @return
     */
    @SysLog("一般工程备案：修改后还是暂存状态")
    @PostMapping("/update")
    public R update(@RequestBody Generecord generecord) {
        String s = "";
        try {
            // 若已经提交，修改后提交状态置为0：我提交
            if (!"0".equals(generecord.getCheckStatus())) {
                generecord.setCheckStatus("0");
            }
            Generecord targetGenerecord = generecordService.queryById(generecord.getReId());
            // 空的值不覆盖原有数据
            BeanUtil.copyBeanNotEmptyToBean(generecord, targetGenerecord);
            System.out.println(targetGenerecord.toString());
            generecordService.update(targetGenerecord);
            s = JSON.toJSONString(targetGenerecord);
            sysAuditService.saveSuccessAudit("工程备案修改成功", Constant.GENERECORD, Constant.PT, Constant.UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("工程备案修改失败", Constant.GENERECORD, Constant.PT, Constant.UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    @SysLog("一般工程备案：修改后是提交状态")
    @PostMapping("/updateSubmit")
    public R updateSubmit(@RequestBody Generecord generecord) {
        String s = "";
        try {
            // 若已经提交，修改后提交状态置为0：我提交
            if ("0".equals(generecord.getCheckStatus())) {
                generecord.setCheckStatus("1");
            }
            Generecord targetGenerecord = generecordService.queryById(generecord.getReId());
            // 空的值不覆盖原有数据
            BeanUtil.copyBeanNotEmptyToBean(generecord, targetGenerecord);
            System.out.println(targetGenerecord.toString());
            generecordService.update(targetGenerecord);
            s = JSON.toJSONString(targetGenerecord);
            sysAuditService.saveSuccessAudit("工程备案修改成功", Constant.GENERECORD, Constant.PT, Constant.UPDATESUBMIT, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("工程备案修改失败", Constant.GENERECORD, Constant.PT, Constant.UPDATESUBMIT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    @SysLog("一般工程备案：审核")
    @PostMapping("/check")
    public R check(@RequestBody Generecord generecord) {
        String s = "";
        try {
            Generecord targetGenerecord = generecordService.queryById(generecord.getReId());
            // 空的值不覆盖原有数据
            BeanUtil.copyBeanNotEmptyToBean(generecord, targetGenerecord);
            generecordService.check(targetGenerecord);
            s = JSON.toJSONString(targetGenerecord);
            sysAuditService.saveSuccessAudit("工程备案审核成功", Constant.GENERECORD, Constant.PT, Constant.REVIEWED, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("工程备案审核失败", Constant.GENERECORD, Constant.PT, Constant.REVIEWED, s, e, request);
            return R.error(e.getMessage());
        }
    }

    @SysLog("一般工程备案：提交")
    @PostMapping("/submit/{id}")
    public R submit(@PathVariable String id) {
        String s = "";
        try {
            // 更改上报状态
            generecordService.updateSubmitStatus(id);
            s = JSON.toJSONString(id);
            sysAuditService.saveSuccessAudit("提交成功", Constant.GENERECORD, Constant.PT, Constant.SUBMIT, s, request);
            return R.ok("操作成功!");
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("提交失败", Constant.GENERECORD, Constant.PT, Constant.SUBMIT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 上报
     *
     * @param id
     * @return
     */
    @SysLog("一般工程备案：上报")
    @PostMapping("/report/{id}")
    public R report(@PathVariable String id) {
        String s = "";
        try {
            // 更改上报状态
            generecordService.updateReportStatus(id);
            s = JSON.toJSONString(id);
            sysAuditService.saveSuccessAudit("上报成功", Constant.GENERECORD, Constant.PT, Constant.REPORT, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("上报失败", Constant.GENERECORD, Constant.YZ, Constant.REPORT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @SysLog("一般工程备案：根据ID逻辑删除")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        String s = "";
        try {
            generecordService.logicDel(id);
            s = JSON.toJSONString(id);
            sysAuditService.saveSuccessAudit("删除成功", Constant.GENERECORD, Constant.PT, Constant.DEL, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("删除失败", Constant.GENERECORD, Constant.YZ, Constant.DEL, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 一般工程，中国地震局用户添加拟制公告
     * @param apPublicEntity
     * @return
     */
    @SysLog("一般工程备案：拟制公告")
    @PostMapping("/addPublick")
    public R addPublick(@RequestBody ApPublicEntity apPublicEntity) {
        String s = "";
        try {
            generecordService.addPublick(apPublicEntity);
            s = JSON.toJSONString(apPublicEntity);
            sysAuditService.saveSuccessAudit("拟制公告成功", Constant.GENERECORD, Constant.PT, Constant.ADDPUBLICK, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("拟制公告失败", Constant.GENERECORD, Constant.YZ, Constant.ADDPUBLICK, s, e, request);
            return R.error(e.getMessage());
        }
    }

}
